/*!
 * vinculum
 * Copyright(c) 2017 azzly
 * MIT Licensed
 */
 
'use strict';

/**
 * Module dependencies.
 * @private
 */
const fs = require("fs");
const vm = require("vm");

/**
 * Variables
 * @private
 */

const ACTIONS_ROOT = __dirname + '/actions/';
const JS_ROOT = __dirname + '/templates/js/';
const CSS_ROOT = __dirname + '/templates/css/';
const HTML_ROOT = __dirname + '/templates/html/';

const TABLE_PREFIX = 'vinculum';
const SYSTEMDBNAME = 'systemdb';

const TABLE_NAMES = {
  ACTION : 'action'
  ,APPLICATION : 'application'
  ,AUTHORIZATION : 'authorization'
  ,CRON : 'cron'
  ,DATASOURCE : 'datasource'
  ,LINKER : 'linker'
  ,LOG : 'log'
  ,PARAMS : 'params'
  ,RESOURCE : 'resource'
  ,ROLE : 'role'
  ,SERVICE : 'service'
  ,THIRD_PARTY : 'third_party'
  ,THIRD_PARTY_AUTH : 'third_party_auth'
  ,USER : 'user'
};

for(let key in TABLE_NAMES){
	TABLE_NAMES[key] = TABLE_PREFIX + '_' + TABLE_NAMES[key];
}

const SESSION_CACHE_TYPE = {
	LOCAL:'local'
	,REDIS:'redis'
};

const DEBUG_TYPES = {
	NONE:0
	,DEBUG:1
};

const DISABLED_TYPES = {
	NONE:0
	,DISABLED:1
};

const RESOURCE_TYPES = {
	LOCAL:1
	,LINK:0
	,REMOTE:2
};

const FILTER_TYPES = {
	NONE:0
	,ROLE:1
	,ROOT:2
	,USER:4
	,APP:8
};

const LOCK_TYPES = {
	ACTION:'action'
	,ACTION_PARAMS:'action_params'
};

const SERVICE_TYPES = {
	SELECT : 1
	,INSERT : 2
	,UPDATE : 4
	,DELETE : 8
};

const LOG_TYPES = {
  NONE:0
  ,LOG:1
};

const LINK_TYPES = {
	SP : 'service_params'
	,UR : 'user_role'
	,AR : 'application_resource'
};

const SQLS = {
	action_browse: `select * from ${TABLE_NAMES.ACTION} where id = ? or name = ?`
	,action_delete: `delete from ${TABLE_NAMES.ACTION} where id = ?`
	,action_insert: `insert into ${TABLE_NAMES.ACTION}(id,name,main,locktype,isfilter,islog,l10n,disabled) values(?,?,?,?,?,?,?,?)`
	,action_list: `select id,name,l10n,isfilter,islog,locktype,createtime,disabled from ${TABLE_NAMES.ACTION} order by name`
	,action_list_count: `select count(*) as allcount from ${TABLE_NAMES.ACTION}`
	,action_update: `update ${TABLE_NAMES.ACTION} set main=?,locktype=?,isfilter=?,islog=?,l10n=?,disabled=? where id = ?`
	,appandresource_copy: `insert into ${TABLE_NAMES.LINKER}(mainid,linkid,linktype,ordernum) select ?,linkid,linktype,ordernum from ${TABLE_NAMES.LINKER} where mainid = ? and linktype = '${LINK_TYPES.AR}'`
	,appandresource_delete_byappid: `delete from ${TABLE_NAMES.LINKER} where mainid = ? and linktype = '${LINK_TYPES.AR}'`
	,appandresource_delete_byresourceid: `delete from  ${TABLE_NAMES.LINKER} where linkid = ? and linktype = '${LINK_TYPES.AR}'`
	,appandresource_insert: `insert into ${TABLE_NAMES.LINKER}(mainid,linkid,linktype,ordernum) select ?,id,'${LINK_TYPES.AR}',ordernum from ${TABLE_NAMES.RESOURCE} where find_in_set(id,?)`
	,application_browse: `select * from ${TABLE_NAMES.APPLICATION} where id = ? or name = ?`
	,application_browse_js_byappid:`(select id,main_js as main from ${TABLE_NAMES.APPLICATION} where id = ?)`
  ,application_browse_css_byappid:`(select id,main_css as main from ${TABLE_NAMES.APPLICATION} where id = ?)`
  ,application_browse_html_byappid:`(select id,main_html as main from ${TABLE_NAMES.APPLICATION} where id = ?)`
	,application_browseandcode: `select id,name,title,isfilter,authurl,description,debug,keywords,islog,before_service,before_action,viewport,disabled,(select main from ${TABLE_NAMES.CODE} where id = a.main_html) as main_html,(select main from ${TABLE_NAMES.CODE} where id = a.main_js) as main_js,(select main from ${TABLE_NAMES.CODE} where id = a.main_css) as main_css from ${TABLE_NAMES.APPLICATION} a where id = ? or name = ?`
	,application_delete: `delete from ${TABLE_NAMES.APPLICATION} where id = ?`
	,application_insert: `insert into ${TABLE_NAMES.APPLICATION}(id,name,title,isfilter,authurl,description,debug,keywords,islog,before_service,before_action,viewport,main_html,main_js,main_css,disabled) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
	,application_list: `select id,name,title,isfilter,islog,authurl,description,debug,disabled,keywords,viewport,before_action,before_service,createtime from ${TABLE_NAMES.APPLICATION} order by disabled,name`
	,application_list_count: `select count(*) as allcount from ${TABLE_NAMES.APPLICATION}`
	,application_update: `update ${TABLE_NAMES.APPLICATION} set title=?,isfilter=?,authurl=?,description=?,debug=?,keywords=?,viewport=?,before_action=?,before_service=?,islog=?,disabled=? where id = ?`
	,application_update_css: `update ${TABLE_NAMES.APPLICATION} set main_css =? where id = ?`
	,application_update_html: `update ${TABLE_NAMES.APPLICATION} set main_html=? where id = ?`
	,application_update_js: `update ${TABLE_NAMES.APPLICATION} set main_js=? where id = ?`
	,authorization_browse: `select * from ${TABLE_NAMES.AUTHORIZATION} where whatid = ? and whattype = ?`
	,authorization_copy: `insert into ${TABLE_NAMES.AUTHORIZATION}(id,roleid,whattype,whatid) select uuid(),roleid,whattype,? from ${TABLE_NAMES.AUTHORIZATION} where whatid = ?`
	,authorization_delete_bywhatid: `delete from ${TABLE_NAMES.AUTHORIZATION} where whatid = ?`
	,authorization_insert: `insert into ${TABLE_NAMES.AUTHORIZATION}(id,roleid,whattype,whatid) select uuid(),id,?,? from ${TABLE_NAMES.ROLE} where find_in_set(id,?)`
	,cron_list: `select * from ${TABLE_NAMES.CRON} where isrun = 1`
	,datasource_list: `select * from ${TABLE_NAMES.DATASOURCE}`
	,log_browse: `select * from ${TABLE_NAMES.LOG} where id = ?`
	,log_insert: `insert into ${TABLE_NAMES.LOG}(id,logname,type,request_ip,userid,request_params,response_params) values(?,?,?,?,?,?,?)`
	,log_list: `select * from ${TABLE_NAMES.LOG}`
	,params_browse_bynameandtype: `select * from ${TABLE_NAMES.PARAMS} where name = ? and type = ?`
	,params_insert: `insert into ${TABLE_NAMES.PARAMS}(id,name,type) values(?,?,?)`
	,resource_browse: `SELECT * FROM ${TABLE_NAMES.RESOURCE} where id = ?`
	,resource_browsebyappid: `SELECT a.* FROM ${TABLE_NAMES.RESOURCE} a inner join ${TABLE_NAMES.LINKER} b on a.id = b.linkid and b.linktype = '${LINK_TYPES.AR}' where mainid = ? order by a.ordernum`
	,resource_browsebyappidandtype: `SELECT a.* FROM ${TABLE_NAMES.RESOURCE} a inner join ${TABLE_NAMES.LINKER} b on a.id = b.linkid and b.linktype = '${LINK_TYPES.AR}' where mainid = ? and sourcetype =? order by a.ordernum`
	,resource_delete: `delete FROM ${TABLE_NAMES.RESOURCE} where id = ?`
	,resource_insert: `insert into ${TABLE_NAMES.RESOURCE}(id,name,version,main,type,filetype,ordernum) values(?,?,?,?,?,?,?)`
	,resource_list: `SELECT id,name,version,type,filetype,ordernum,createtime,(case when type=1 then concat(substr(main,1,50),'...') else main end) as main FROM ${TABLE_NAMES.RESOURCE} order by ordernum`
	,resource_list_byappid: `SELECT a.id,a.name,a.version,a.type,a.filetype,a.ordernum,a.createtime,(case when a.type=1 then concat(substr(a.main,1,50),'...') else a.main end) as main FROM ${TABLE_NAMES.RESOURCE} a inner join ${TABLE_NAMES.LINKER} b on a.id = b.linkid and linktype = '${LINK_TYPES.AR}' and mainid = ? order by a.ordernum`
	,resource_list_count: `SELECT count(*) as allcount FROM ${TABLE_NAMES.RESOURCE} order by ordernum`
	,resource_update: `update ${TABLE_NAMES.RESOURCE} set name=?,version=?,main=?,type=?,filetype=?,ordernum=? where id = ?`
	,role_browse: `select * from ${TABLE_NAMES.ROLE} where id = ?`
	,role_delete: `delete from ${TABLE_NAMES.ROLE} where id=?`
	,role_insert: `insert into ${TABLE_NAMES.ROLE}(id,name,l10n) values(?,?,?)`
	,role_list: `select * from ${TABLE_NAMES.ROLE} order by name`
	,role_list_byauth: `select a.* from ${TABLE_NAMES.ROLE} a inner join ${TABLE_NAMES.AUTHORIZATION} b on a.id = b.roleid and b.whatid = ? and b.whattype = ? order by a.name`
	,role_list_byuser: `select a.* from ${TABLE_NAMES.ROLE} a inner join ${TABLE_NAMES.LINKER} b on a.id = b.linkid and b.linktype = '${LINK_TYPES.UR}' and b.mainid = ? order by a.name`
	,role_list_count: `select count(*) as allcount from ${TABLE_NAMES.ROLE}`
	,role_update: `update ${TABLE_NAMES.ROLE} set l10n=? where id = ?`
	,service_browse: `select aa.*,(select group_concat(a.fullname order by b.ordernum) from (select id,(case when type = 'form' then name else concat('__',type,'__',name) end) as fullname from ${TABLE_NAMES.PARAMS}) a inner join ${TABLE_NAMES.LINKER} b on a.id = b.linkid and b.linktype='${LINK_TYPES.SP}' where b.mainid = aa.id ) as serviceparams from ${TABLE_NAMES.SERVICE} aa where id = ? or name = ?`
	,service_delete: `delete from ${TABLE_NAMES.SERVICE} where id = ?`
	,service_insert: `insert into ${TABLE_NAMES.SERVICE}(id,name,main,type,isfilter,islog,datasourceid,l10n,disabled) values(?,?,?,?,?,?,?,?,?)`
	,service_list: `select * from ${TABLE_NAMES.SERVICE} order by name`
	,service_list_count: `select count(*) as allcount from ${TABLE_NAMES.SERVICE}`
	,service_param_browse: `select b.* from ${TABLE_NAMES.LINKER} a inner join ${TABLE_NAMES.PARAMS} b on a.linkid = b.id where a.mainid = ? order by a.ordernum`
	,service_param_insert: `insert into ${TABLE_NAMES.LINKER}(mainid,linkid,linktype,ordernum) values(?,?,'${LINK_TYPES.SP}',?)`
	,service_params_delete_byserviceid: `delete from  ${TABLE_NAMES.LINKER} where mainid = ? and linktype='${LINK_TYPES.SP}'`
	,service_update: `update ${TABLE_NAMES.SERVICE} set main=?,type=?,isfilter=?,islog=?,datasourceid=?,l10n=?,disabled=? where id = ?`
	,third_party_browse: `select a.* from ${TABLE_NAMES.THIRD_PARTY} a inner join ${TABLE_NAMES.THIRD_PARTY_AUTH} b on a.appid = b.appid where now() BETWEEN starttime and endtime and b.appid = ? and b.whatid = ?  and b.whattype = ?`
	,user_browse: `select id,username,createtime from ${TABLE_NAMES.USER} where id = ?`
	,user_browse_bynameandpwd: `select * from ${TABLE_NAMES.USER} where username = ? and userpwd = ?`
	,user_delete: `delete from ${TABLE_NAMES.USER} where id = ?`
	,user_insert: `insert into ${TABLE_NAMES.USER}(id,username,userpwd) values(?,?,?)`
	,user_list: `select * from ${TABLE_NAMES.USER} order by username`
	,user_list_count: `select count(*) as allcount from ${TABLE_NAMES.USER}`
	,user_role_browse_byuserid: `select mainid as userid,linkid as roleid from ${TABLE_NAMES.LINKER} where mainid = ? and linktype='${LINK_TYPES.UR}'`
	,user_role_delete_byroleid: `delete from ${TABLE_NAMES.LINKER} where linkid = ? and linktype='${LINK_TYPES.UR}'`
	,user_role_delete_byuserid: `delete from ${TABLE_NAMES.LINKER} where mainid = ? and linktype='${LINK_TYPES.UR}'`
	,user_role_insert: `insert into ${TABLE_NAMES.LINKER}(mainid,linkid,linktype,ordernum) select ?,id,'${LINK_TYPES.UR}',0 from ${TABLE_NAMES.ROLE} where find_in_set(id,?)`
	,user_update: `update ${TABLE_NAMES.USER} set userpwd=? where id = ?`
};

const CONST_SERVICES = { 
	action_browse: { param_list: [ { name: 'id' }, { name: 'name' } ], type: SERVICE_TYPES.SELECT}
	,action_delete: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.DELETE}
	,action_insert: { param_list: [ { name: 'id', type: 'key' },  { name: 'name' },  { name: 'main' },  { name: 'locktype' },  { name: 'isfilter' },  { name: 'islog' },  { name: 'l10n' },  { name: 'disabled' } ], type: SERVICE_TYPES.INSERT}
	,action_list: { param_list: [], type: SERVICE_TYPES.SELECT}
	,action_list_count: { param_list: [], type: SERVICE_TYPES.SELECT}
	,action_update: { param_list: [ { name: 'main' },  { name: 'locktype' },  { name: 'isfilter' },  { name: 'islog' },  { name: 'l10n' },  { name: 'disabled' },  { name: 'id' } ], type: SERVICE_TYPES.UPDATE}
	,appandresource_copy: { param_list: [ { name: 'new_appid' }, { name: 'appid' } ], type: SERVICE_TYPES.INSERT}
	,appandresource_delete_byappid: { param_list: [ { name: 'appid' } ], type: SERVICE_TYPES.DELETE}
	,appandresource_delete_byresourceid: { param_list: [ { name: 'resourceid' } ], type: SERVICE_TYPES.DELETE}
	,appandresource_insert: { param_list: [ { name: 'appid' }, { name: 'resourceids' } ], type: SERVICE_TYPES.INSERT}
	,application_browse: { param_list: [ { name: 'id' }, { name: 'name' } ], type: SERVICE_TYPES.SELECT}
	,application_browse_css_byappid: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.SELECT}
	,application_browse_html_byappid: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.SELECT}
	,application_browse_js_byappid: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.SELECT}
	,application_browseandcode: { param_list: [ { name: 'id' }, { name: 'name' } ], type: SERVICE_TYPES.SELECT}
	,application_delete: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.DELETE}
	,application_insert: { param_list: [ { name: 'id', type: 'key' },  { name: 'name' },  { name: 'title' },  { name: 'isfilter' },  { name: 'authurl' },  { name: 'description' },  { name: 'debug' },  { name: 'keywords' },  { name: 'islog' },  { name: 'before_service' },  { name: 'before_action' },  { name: 'viewport' },  { name: 'main_html' },  { name: 'main_js' },  { name: 'main_css' },  { name: 'disabled' } ], type: SERVICE_TYPES.INSERT}
	,application_list: { param_list: [], type: SERVICE_TYPES.SELECT}
	,application_list_count: { param_list: [], type: SERVICE_TYPES.SELECT}
	,application_update: { param_list: [ { name: 'title' },  { name: 'isfilter' },  { name: 'authurl' },  { name: 'description' },  { name: 'debug' },  { name: 'keywords' },  { name: 'viewport' },  { name: 'before_action' },  { name: 'before_service' },  { name: 'islog' },  { name: 'disabled' },  { name: 'id' } ], type: SERVICE_TYPES.UPDATE}
	,application_update_css: { param_list: [ { name: 'main' }, { name: 'id' } ], type: SERVICE_TYPES.UPDATE}
	,application_update_html: { param_list: [ { name: 'main' }, { name: 'id' } ], type: SERVICE_TYPES.UPDATE}
	,application_update_js: { param_list: [ { name: 'main' }, { name: 'id' } ], type: SERVICE_TYPES.UPDATE}
	,authorization_browse: { param_list: [ { name: 'whatid' }, { name: 'whattype' } ], type: SERVICE_TYPES.SELECT}
	,authorization_copy: { param_list: [ { name: 'new_whatid' }, { name: 'whatid' } ], type: SERVICE_TYPES.INSERT}
	,authorization_delete_byroleid: { param_list: [ { name: 'roleid' } ], type: SERVICE_TYPES.DELETE}
	,authorization_delete_bywhatid: { param_list: [ { name: 'whatid' } ], type: SERVICE_TYPES.DELETE}
	,authorization_insert: { param_list: [ { name: 'whattype' }, { name: 'whatid' }, { name: 'roleids' } ], type: SERVICE_TYPES.INSERT}
	,cron_list: { param_list: [], type: SERVICE_TYPES.SELECT}
	,datasource_list: { param_list: [], type: SERVICE_TYPES.SELECT}
	,log_browse: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.SELECT}
	,log_insert: { param_list: [ { name: 'id', type: 'key' },  { name: 'logname' },  { name: 'type' },  { name: 'ip', type: 'request_header' },  { name: 'id', type: 'session_user' },  { name: 'request_params' },  { name: 'response_params' } ], type: SERVICE_TYPES.INSERT}
	,log_list: { param_list: [], type: SERVICE_TYPES.SELECT}
	,params_browse_bynameandtype: { param_list: [ { name: 'name' }, { name: 'type' } ], type: SERVICE_TYPES.SELECT}
	,params_insert: { param_list: [ { name: 'id', type: 'key' },  { name: 'name' },  { name: 'type' } ], type: SERVICE_TYPES.INSERT}
	,resource_browse: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.SELECT}
	,resource_browsebyappid: { param_list: [ { name: 'appid' } ], type: SERVICE_TYPES.SELECT}
	,resource_browsebyappidandtype: { param_list: [ { name: 'appid' }, { name: 'sourcetype' } ], type: SERVICE_TYPES.SELECT}
	,resource_delete: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.DELETE}
	,resource_insert: { param_list: [ { name: 'id', type: 'key' },  { name: 'name' },  { name: 'version' },  { name: 'main' },  { name: 'type' },  { name: 'filetype' },  { name: 'ordernum' } ], type: SERVICE_TYPES.INSERT}
	,resource_list: { param_list: [], type: SERVICE_TYPES.SELECT}
	,resource_list_byappid: { param_list: [ { name: 'appid' } ], type: SERVICE_TYPES.SELECT}
	,resource_list_count: { param_list: [], type: SERVICE_TYPES.SELECT}
	,resource_update: { param_list: [ { name: 'name' },  { name: 'version' },  { name: 'main' },  { name: 'type' },  { name: 'filetype' },  { name: 'ordernum' },  { name: 'id' } ], type: SERVICE_TYPES.UPDATE}
	,role_browse: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.SELECT}
	,role_delete: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.DELETE}
	,role_insert: { param_list: [ { name: 'id', type: 'key' },  { name: 'name' },  { name: 'l10n' } ], type: SERVICE_TYPES.INSERT}
	,role_list: { param_list: [], type: SERVICE_TYPES.SELECT}
	,role_list_byauth: { param_list: [ { name: 'whatid' }, { name: 'whattype' } ], type: SERVICE_TYPES.SELECT}
	,role_list_byuser: { param_list: [ { name: 'userid' } ], type: SERVICE_TYPES.SELECT}
	,role_list_count: { param_list: [], type: SERVICE_TYPES.SELECT}
	,role_update: { param_list: [ { name: 'l10n' }, { name: 'id' } ], type: SERVICE_TYPES.UPDATE}
	,service_browse: { param_list: [ { name: 'id' }, { name: 'name' } ], type: SERVICE_TYPES.SELECT}
	,service_delete: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.DELETE}
	,service_insert: { param_list: [ { name: 'id', type: 'key' },  { name: 'name' },  { name: 'main' },  { name: 'type' },  { name: 'isfilter' },  { name: 'islog' },  { name: 'datasourceid' },  { name: 'l10n' },  { name: 'disabled' } ], type: SERVICE_TYPES.INSERT}
	,service_list: { param_list: [], type: SERVICE_TYPES.SELECT}
	,service_list_count: { param_list: [], type: SERVICE_TYPES.SELECT}
	,service_param_insert: { param_list: [ { name: 'serviceid' },  { name: 'paramid' },  { name: 'ordernum' } ], type: SERVICE_TYPES.INSERT}
	,service_params_delete_byserviceid: { param_list: [ { name: 'serviceid' } ], type: SERVICE_TYPES.DELETE}
	,service_update: { param_list: [ { name: 'main' },  { name: 'type' },  { name: 'isfilter' },  { name: 'islog' },  { name: 'datasourceid' },  { name: 'l10n' },  { name: 'disabled' },  { name: 'id' } ], type: SERVICE_TYPES.UPDATE}
	,service_param_browse: { param_list: [ { name: 'serviceid' } ], type: SERVICE_TYPES.SELECT}
	,third_party_browse: { param_list: [ { name: 'appid' }, { name: 'whatid' }, { name: 'whattype' } ], type: SERVICE_TYPES.SELECT}
	,user_browse: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.SELECT}
	,user_browse_bynameandpwd: { param_list: [ { name: 'username' }, { name: 'userpwd', type: 'md5' } ], type: SERVICE_TYPES.SELECT}
	,user_delete: { param_list: [ { name: 'id' } ], type: SERVICE_TYPES.DELETE}
	,user_insert: { param_list: [ { name: 'id', type: 'key' },  { name: 'username' },  { name: 'userpwd', type: 'md5' } ], type: SERVICE_TYPES.INSERT}
	,user_list: { param_list: [], type: SERVICE_TYPES.SELECT}
	,user_list_count: { param_list: [], type: SERVICE_TYPES.SELECT}
	,user_role_browse_byuserid: { param_list: [ { name: 'userid' } ], type: SERVICE_TYPES.SELECT}
	,user_role_delete_byroleid: { param_list: [ { name: 'roleid' } ], type: SERVICE_TYPES.DELETE}
	,user_role_delete_byuserid: { param_list: [ { name: 'userid' } ], type: SERVICE_TYPES.DELETE}
	,user_role_insert: { param_list: [ { name: 'userid' }, { name: 'roleids' } ], type: SERVICE_TYPES.INSERT}
	,user_update: { param_list: [ { name: 'userpwd', type: 'md5' }, { name: 'id' } ], type: SERVICE_TYPES.UPDATE}
};

let static_services = { };

for(let key in CONST_SERVICES){
  let oneservice = CONST_SERVICES[key];
  if(!oneservice.isfilter){
    oneservice.isfilter = FILTER_TYPES.ROOT;
  }
  if(!oneservice.datasourceid){
    oneservice.datasourceid = SYSTEMDBNAME;
  }
  oneservice.main = SQLS[key];
  oneservice.islog = LOG_TYPES.NONE;
  static_services[`${TABLE_PREFIX}_${key}`] = oneservice;
}

const REG = {
	NUMBER:/^[0-9]+$/
	,MOBILE:/^1[34578][0-9]{9}$/
	,CARD15:/^([1-9]\d{5})(\d{2})(0[1-9]|10|11|12)([0-2][1-9]|10|20|30|31)(\d{3})$/
	,CARD18:/^([1-9]\d{5})((19|20|21)\d{2})(0[1-9]|10|11|12)([0-2][1-9]|10|20|30|31)(\d{3}[0-9Xx])$/
	,URL: /((http|ftp|https):\/\/)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(\/[a-zA-Z0-9\&%_\.\/-~-]*)?/i
};

const ACTIONS = {
  application_insert:`${ACTIONS_ROOT}application_insert.js`
  ,application_delete:`${ACTIONS_ROOT}application_delete.js`
  ,application_update:`${ACTIONS_ROOT}application_update.js`
  ,application_copy:`${ACTIONS_ROOT}application_copy.js`
  ,authorization_update:`${ACTIONS_ROOT}authorization_update.js`
  ,appandresource_update:`${ACTIONS_ROOT}appandresource_update.js`
  ,login:`${ACTIONS_ROOT}login.js`
  ,service_insert:`${ACTIONS_ROOT}service_insert.js`
  ,service_copy:`${ACTIONS_ROOT}service_copy.js`
  ,service_delete:`${ACTIONS_ROOT}service_delete.js`
  ,service_update:`${ACTIONS_ROOT}service_update.js`    
  ,action_insert:`${ACTIONS_ROOT}action_insert.js`
  ,action_copy:`${ACTIONS_ROOT}action_copy.js`
  ,action_delete:`${ACTIONS_ROOT}action_delete.js`
  ,action_update:`${ACTIONS_ROOT}action_update.js`    
  ,user_insert:`${ACTIONS_ROOT}user_insert.js`
  ,user_delete:`${ACTIONS_ROOT}user_delete.js`
  ,user_update:`${ACTIONS_ROOT}user_update.js`    
  ,user_role_update:`${ACTIONS_ROOT}user_role_update.js`    
  ,role_insert:`${ACTIONS_ROOT}role_insert.js`
  ,role_delete:`${ACTIONS_ROOT}role_delete.js`
  ,role_update:`${ACTIONS_ROOT}role_update.js`   
  ,resource_insert:`${ACTIONS_ROOT}resource_insert.js`
  ,resource_copy:`${ACTIONS_ROOT}resource_copy.js`
  ,resource_delete:`${ACTIONS_ROOT}resource_delete.js`
  ,resource_update:`${ACTIONS_ROOT}resource_update.js`
};

let static_actions = {};

for(let key in ACTIONS){
  let actionprop = {
    isfilter:FILTER_TYPES.ROOT
    ,locktype:LOCK_TYPES.ACTION
    ,main:fs.readFileSync(ACTIONS[key],'utf8')
    ,islog:LOG_TYPES.NONE
  };
  
  if(key === 'login'){
    actionprop.isfilter = FILTER_TYPES.NONE;
  }
	
	actionprop.script = new vm.Script(`try{
		${actionprop.main}
	}catch(err){
		ret(err.message);
	}`);
  
  static_actions[`${TABLE_PREFIX}_${key}`] = actionprop;
}


const libs = [
	'ionicons.min.css'
	,'bootstrap.min.css'
	//,'amazeui.min.css'
	,
];

function resource_map_fun(one){
	var filetype = one.split('.').pop();
	var rootpath = '../resource/' + filetype + '/';
	var ret = {filetype:filetype};
	ret.type = RESOURCE_TYPES.LINK;
	ret.main = rootpath + one;
	return ret;
}

const console_resources = libs.concat(['ace.js'
,'mode-css.js'
,'mode-javascript.js'
,'mode-mysql.js'
,'mode-html.js'
,'mode-less.js'
,'worker-css.js'
,'worker-javascript.js'
,'worker-html.js'
,'theme-twilight.js'
,'ext-statusbar.js'
,'console.js'
,'console.css']).map(resource_map_fun);

const login_resources = libs.concat(['login.css','login.js']).map(resource_map_fun);

const application_info = {
  'console':{
    id:'console'
    ,name:'console'
    ,title:'管理后台'
    ,isfilter:FILTER_TYPES.ROOT
    ,authurl:null
    ,description:'管理后台'
    ,debug:DEBUG_TYPES.DEBUG
    ,islog:LOG_TYPES.LOG
    ,keywords:'管理后台'
    ,viewport:'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no'
    ,main_html:fs.readFileSync(HTML_ROOT + 'console.html','utf8')
		,resources:console_resources
  }
  ,'login':{
    id:'login'
    ,name:'login'
    ,title:'登录'
    ,isfilter:FILTER_TYPES.NONE
    ,authurl:null
    ,description:'登录'
    ,debug:DEBUG_TYPES.DEBUG
    ,islog:LOG_TYPES.LOG
    ,keywords:'登录'
    ,viewport:'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no'
    ,main_html:fs.readFileSync(HTML_ROOT + 'login.html','utf8')
		,resources:login_resources
  }
};
 
/**
 * prototype.
 */

let obj = exports = module.exports = {
	SESSION_CACHE_TYPE : SESSION_CACHE_TYPE
	,DEBUG_TYPES : DEBUG_TYPES
	,LOG_TYPES : LOG_TYPES
	,DISABLED_TYPES : DISABLED_TYPES
	,RESOURCE_TYPES : RESOURCE_TYPES
	,FILTER_TYPES : FILTER_TYPES
	,LOCK_TYPES : LOCK_TYPES
	,SERVICE_TYPES : SERVICE_TYPES
	,TABLE_PREFIX:TABLE_PREFIX
	,TABLE_NAMES:TABLE_NAMES
	,REG : REG
	,SQLS : SQLS
	,SYSTEMDBNAME : SYSTEMDBNAME
	,ACTIONS:static_actions
	,SERVICES:static_services
  ,APPLICATIONS:application_info
};
